<?php
namespace core\lib;
class Route
{
    public $controller; // 控制器
    public $action; // 方法(动作)
    public function __construct() {
        // xxx.com/index.php/index/index
        // xxx.com/index.php/index
        /*
     * 1.隐藏index.php
     * 2.获取URL 参数部分
     * 3.返回对应控制器和方法
     * */
        if (isset($_SERVER['REQUEST_URI']) && $_SERVER['REQUEST_URI'] != '/') {
            // 处理成这种格式:index/index
            $path = $_SERVER['REQUEST_URI'];
            $pathArr = explode('/', trim($path, '/'));

            if (isset($pathArr[0])) {
                $this->controller = $pathArr[0];
                $pat[0] = $pathArr[0];
            }
            unset($pathArr[0]);
            if (isset($pathArr[1])) {
                $this->action = $pathArr[1];
                $pat[1] = $pathArr[1];
                unset($pathArr[1]);
            } else {
                $this->action = 'index';

                //ko
                $pat[1] = 'index';
                //kop

            }
            if (isset($pathArr[2])) {
                $this->module = $pathArr[2];
                $pat[2] = $pathArr[2];
            }else{
                $this->module = 'index';

                //ko
                $pat[2] = 'index';
                //kop
            }
            unset($pathArr[2]);
            // url多余部分(参数部分)转换成 GET
            // id/1/str/2
            
            for ($i = 2; $i < count($pathArr) + 2; $i++) {
                if (isset($pathArr[$i + 1])) {
                    $_GET["lp".($i-2)]=$pathArr[$i + 1];
                }
            }
            
            
            //p($this);
            //p($_GET); // 打印GET
        } else {
            $this->controller = 'index'; // 默认控制器
            $this->action = 'index'; // 默认方法
            $this->module = 'index'; // 默认模块
            $pat[0] = 'index';
            $pat[1] = 'index';
            $pat[2] = 'index';
        }
if(file_exists(CORE.'/val/'.$pat[1].'/'.$pat[2].'.php')){
    include CORE.'/val/'.$pat[1].'/'.$pat[2].'.php';
}else{
    echo '<!DOCTYPE html><html><head><meta charset="utf-8"><title>404</title><script src="https://volunteer.cdn-go.cn/404/latest/404.js"></script></head></html>';
}






    }
}